﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace CHILIS.Componentes.Adquicisiones.AspNetForms
{
    public partial class aspnetCRViewer : System.Web.UI.Page
    {

        /// <summary>
        /// This is generic report viewer
        /// This ReportViewer will load report with the report name Session["ReportName"]
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                bool isValid = true;

                // Setting ReportName
                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
                int nroOC = (int)Session["nroOrdenCompra"];
                // Setting FromDate 
                //string strNumeroOC = System.Web.HttpContext.Current.Session["rptNumeroOC"].ToString();
                // Setting Report Data Source     
                var rptSource = System.Web.HttpContext.Current.Session["rptSource"];

                if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
                {
                    isValid = false;
                }


                if (isValid) // If Report Name provided then do other operation
                {
                    var rd = new ReportDocument();
                    string strRptPath = Server.MapPath("~/") + "Reports//" + strReportName;
                    //Loading Report
                    rd.Load(strRptPath);

                    // Setting report data source
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);

                    //if (!string.IsNullOrEmpty(strNumeroOC))
                    //    rd.SetParameterValue("fromDate", strNumeroOC);

                    ExportOptions CrExportOptions;
                    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                    CrDiskFileDestinationOptions.DiskFileName = Server.MapPath("~/") + "file//" + nroOC +  ".pdf";
                    CrExportOptions = rd.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    rd.Export();



                    CrystalReportViewer1.ReportSource = rd;
                    CrystalReportViewer1.DataBind();

                    //Session["ReportName"] = "";
                    ////Session["rptNumeroOC"] = "";
                    //Session["rptSource"] = "";
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}